package cmp.services;

import java.sql.Date;
import java.sql.Timestamp;

import javax.ws.rs.Consumes;
import javax.ws.rs.CookieParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;

import cmp.db.DatabaseManager;
import cmp.to.ComplaintTO;
import cmp.to.DeptTO;
import cmp.to.LoginResponseTO;
import cmp.to.UserTO;

@Path("/service")
public class RestService {

	@POST
	@Path("/login")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String login(@FormParam("id") String id, @FormParam("pwd") String pwd) {
		LoginResponseTO response = new LoginResponseTO();
		
		String res = null;
		//System.out.println("id : " + id + " pwd : " + pwd);

		DatabaseManager db = new DatabaseManager();
		response = db.getLogin(id, pwd);

		Gson gson = new Gson();
		res = gson.toJson(response);
		return res;
	}

	@POST
	@Path("createUser")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public String createUser(@FormParam("name") String name, @FormParam("dob") String dob, @FormParam("address") String address,
			@FormParam("phone_no") String phone_no, @FormParam("pwd") String pwd) {
		String response = null;
		UserTO to = new UserTO();
		Gson gson = new Gson();
		
		to.setName(name);
		to.setDob(Date.valueOf(dob));
		to.setAddress(address);
		to.setPhone_no(phone_no);
		to.setPwd(pwd);

		DatabaseManager db = new DatabaseManager();
		to = db.createUser(to);
		
		response = gson.toJson(to);
		return response;
	}
	
	@POST
	@Path("createDept")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public String createUser(@FormParam("dept_name") String dept_name, @FormParam("dept_pwd") String dept_pwd) {
		String response = null;
		DeptTO to = new DeptTO();
		Gson gson = new Gson();
		
		to.setDept_name(dept_name);
		to.setDept_pwd(dept_pwd);

		DatabaseManager db = new DatabaseManager();
		to = db.createDept(to);
		
		response = gson.toJson(to);
		return response;
	}
	
	@GET
	@Path("getDepartments")
	@Produces(MediaType.APPLICATION_JSON)
	public String getDepartments() {
		String response = null;
		Gson gson = new Gson();

		DatabaseManager db = new DatabaseManager();
		response = gson.toJson(db.getDepartments());

		return response;
	}
	
	@POST
	@Path("getComplaints")
	@Produces(MediaType.APPLICATION_JSON)
	public String getComplaints(@FormParam("field")String field, @FormParam("key")String key, @CookieParam("loginId") int id,@CookieParam("loginType") String userType) {
		String response = null;
		Gson gson = new Gson();
		
		//System.out.println("field : " + field);
		//System.out.println("key : " + key);
		//System.out.println("id : " + id);
		//System.out.println("userType : " + userType);

		DatabaseManager db = new DatabaseManager();
		response = gson.toJson(db.getComplaints(field, key, id, userType));

		return response;
	}
	
	@POST
	@Path("searchComplaints")
	@Produces(MediaType.APPLICATION_JSON)
	public String searchComplaints(@FormParam("field")String field, @FormParam("value1")String value) {
		String response = null;
		Gson gson = new Gson();
		
		System.out.println("field : " + field);
		System.out.println("value : " + value);

		DatabaseManager db = new DatabaseManager();
		response = gson.toJson(db.searchComplaints(field, value));

		return response;
	}
	
	
	@POST
	@Path("getComplaintById")
	@Produces(MediaType.APPLICATION_JSON)
	public String getComplaints(@FormParam("cid")int cid) {
		String response = null;
		Gson gson = new Gson();
		
		System.out.println("cid :" + cid);


		DatabaseManager db = new DatabaseManager();
		response = gson.toJson(db.getComplaintById(cid));

		return response;
	}

	@POST
	@Path("createComplaint")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String createComplaint(@CookieParam("loginId") int uid, @FormParam("did") int did, @FormParam("location") String loc,
			@FormParam("desc") String desc) {
		
//		System.out.println("uid : " + uid);
//		System.out.println("did : " + did);
//		System.out.println("location : " + loc);
//		System.out.println("desc : " + desc);
//		java.sql.Timestamp date = new java.sql.Timestamp(new java.util.Date().getTime());
//		System.out.println("date : " + date.toString());
		
		ComplaintTO to = new ComplaintTO();
		to.setDate_submission(new java.sql.Timestamp(new java.util.Date().getTime()));
		to.setDescription(desc);
		to.setDid(did);
		to.setLocation(loc);
		to.setUid(uid);
		
		DatabaseManager db = new DatabaseManager();

		return db.createComplaint(to);

	}
	
	@POST
	@Path("updateComplaint")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String updateComplaint(@CookieParam("loginId") int uid, @FormParam("did") int did, @FormParam("location") String loc,
			@FormParam("desc") String desc, @FormParam("cid") int cid) {

		System.out.println("cid : " + cid);
		
		ComplaintTO to = new ComplaintTO();
		to.setDescription(desc);
		to.setDid(did);
		to.setLocation(loc);
		to.setUid(uid);
		to.setCid(cid);
		
		DatabaseManager db = new DatabaseManager();
		if( db.updateComplaint(to) )
			return "true";
		else
			return "false";
	}
	
	@POST
	@Path("resolveComplaint")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String resolveComplaint(@CookieParam("loginId") int id, @FormParam("cid") int cid, @FormParam("location") String loc,
			@FormParam("desc") String desc) {
		
//		System.out.println("uid : " + uid);
//		System.out.println("did : " + did);
//		System.out.println("location : " + loc);
//		System.out.println("desc : " + desc);
//		java.sql.Timestamp date = new java.sql.Timestamp(new java.util.Date().getTime());
//		System.out.println("date : " + date.toString());
		
		ComplaintTO to = new ComplaintTO();
		to.setCid(cid);
		to.setDate_resolution(new java.sql.Timestamp(new java.util.Date().getTime()));
		to.setDescription(desc);
		to.setDid(id);
		to.setLocation(loc);
		
		DatabaseManager db = new DatabaseManager();
		if(db.resolveComplaint(to)) return "true";
		return "false";
		
	}
	
	@GET
	@Path("getReportByDept")
	@Produces(MediaType.APPLICATION_JSON)
	public String getReportByDept() {
		String response = null;
		Gson gson = new Gson();

		DatabaseManager db = new DatabaseManager();
		response = gson.toJson(db.getReportByDept());

		return response;
	}
	
	
	@GET
	@Path("getReportByResolutionTime")
	@Produces(MediaType.APPLICATION_JSON)
	public String getReportByResolutionTime() {
		String response = null;
		Gson gson = new Gson();

		DatabaseManager db = new DatabaseManager();
		response = gson.toJson(db.getReportByResolutionTime());

		return response;
	}
	
	//manish..........................
		@POST
		@Path("openComplaint")
		@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
		public String openComplaint(@CookieParam("loginId") int id, @FormParam("cid") int cid, @FormParam("location") String loc,
				@FormParam("desc") String desc) {
			ComplaintTO to = new ComplaintTO();
			to.setCid(cid);
			DatabaseManager db = new DatabaseManager();
			if(db.openComplaint(to)) return "true";
			return "false";
			
		}
		
		@POST
		@Path("getNotification")
		@Produces(MediaType.APPLICATION_JSON)
		public String getNotification(@FormParam("field")String field, @FormParam("key")String key, @CookieParam("loginId") int id,@CookieParam("loginType") String userType) {
			String response = null;
			Gson gson = new Gson();
			
			System.out.println("field : " + field);
			System.out.println("key : " + key);
			System.out.println("id : " + id);
			System.out.println("userType : " + userType);

			DatabaseManager db = new DatabaseManager();
			response = gson.toJson(db.getNotification(field, key, id, userType));

			return response;
		}
		
		

		@POST
		@Path("getdeptNotification")
		@Produces(MediaType.APPLICATION_JSON)
		public String getdeptNotification(@FormParam("field")String field, @FormParam("key")String key, @CookieParam("loginId") int id,@CookieParam("loginType") String userType) {
			String response = null;
			Gson gson = new Gson();
			
			System.out.println("field : " + field);
			System.out.println("key : " + key);
			System.out.println("id : " + id);
			System.out.println("userType : " + userType);

			DatabaseManager db = new DatabaseManager();
			response = gson.toJson(db.getdeptNotification(field, key, id, userType));

			return response;
		}
		
		//.................................

}
